package com.smartguard.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smartguard.bean.Device;
import com.smartguard.bean.User;
import com.smartguard.common.Const;
import com.smartguard.service.DeviceService;
import com.smartguard.service.UserService;
import com.smartguard.util.JSONUtil;

public class UserAction extends BaseAction {
	
	private String username;		//登录、注册时的用户名
	private String password;		//登录、注册时的密码
//	private Device device;			//客户端登录的设备
	
	private UserService userService;
	private DeviceService deviceService;
	
	private String oldPassword;		//修改密码时的旧密码
	private String newPassword;		//修改密码时的新密码
	
	public UserAction(){
		this.userService = new UserService();
	}
	
	/**
	 * 登录
	 * @throws IOException
	 */
	public void login() throws IOException{
		if(username==null || password==null || username.length()<6 || password.length()<6){
			JSONObject json = JSONUtil.newErrMsgJSONObject("用户名或密码长度不能少于6");
			this.getWriter().write(json.toJSONString());
			return;
		}
		
		User ref = new User();
		ref.setUsername(username);
		ref.setPassword(password);
		
		User user = this.userService.login(ref);
		if(user == null){	//登录失败
			JSONObject json = JSONUtil.newErrMsgJSONObject("用户名或密码不正确");
			this.getWriter().write(json.toJSONString());
		}
		else{		//登录成功
			
			// 150810
//			if(device != null){
//				/**
//				 * 检测当前登录设备是否已经存到数据库里了，如果没有则存到数据库中
//				 */
//				device.setUsername(username);	//将这个设备归属于当前登录用户
//				if(this.deviceService.findOneDevice(device) == null){
//					this.deviceService.insertDevice(device);	//向数据库中插入该设备
//				}
//				
//				/**
//				 * 获取当前用户在线的设备列表，并将当前登录设备加入列表中
//				 */
//				CopyOnWriteArrayList<Device> deviceList = this.getUserOnlineDevice(username);
//				
//				//将当前登录设备添加到在线设备列表中
//				if(!deviceList.contains(device)) {
//					deviceList.add(device);
//				}			
//			}
			
			//将当前登录用户放到session里
			this.getSession().put(Const.SESSION_USER, user);
			
			//向客户端返回登录成功信息
			JSONObject json = JSONUtil.newSuccessJSONObject();
			json.put("user", JSON.toJSON(user).toString());
			this.getWriter().write(json.toJSONString());
		}
		
	}
	
	/**
	 * 注册
	 * @throws IOException
	 */
	public void register() throws IOException{
		if(username==null || password==null || username.length()<6 || password.length()<6){
			JSONObject json = JSONUtil.newErrMsgJSONObject("用户名或密码长度不能少于6");
			this.getWriter().write(json.toJSONString());
			return;
		}
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		
		boolean result = this.userService.register(user);
		
		if(result){	//注册成功
//			if(device != null){
//				/**
//				 * 检测当前注册设备是否已经存到数据库里了，如果没有则存到数据库中
//				 */
//				device.setUsername(username);	//将这个设备归属于当前登录用户
//				if(this.deviceService.findOneDevice(device) == null){
//					this.deviceService.insertDevice(device);	//向数据库中插入该设备
//				}
//			}
			this.getSession().put(Const.SESSION_USER, user);
			JSONObject json = JSONUtil.newSuccessJSONObject();
			json.put("user", JSON.toJSON(user).toString());
			this.getWriter().write(json.toJSONString());
		} else {
			JSONObject json = JSONUtil.newErrMsgJSONObject("用户名已被注册");
			this.getWriter().write(json.toJSONString());
		}
	}
	
	/**
	 * 登出
	 * @throws IOException 
	 */
	public void logout() throws IOException{
		this.getSession().clear();
		JSONObject json = JSONUtil.newSuccessJSONObject();
		PrintWriter out = this.getWriter();
		out.write(json.toJSONString());
	}
	
	/**
	 * 更改密码
	 * @throws IOException
	 */
	public void changePassword() throws IOException{
		PrintWriter out = this.getWriter();
		User user = (User) this.getSession().get(Const.SESSION_USER);
		if(user == null){
			out.write(JSONUtil.newErrMsgJSONObject("未登录").toJSONString());
		}
		else {
			if(this.oldPassword !=null 
						&& !"".equals(this.oldPassword)
						&& this.newPassword != null
						&& !"".equals(this.newPassword) ){
				if(!this.oldPassword.equals(user.getPassword())){
					out.write(JSONUtil.newErrMsgJSONObject("原密码不正确！").toJSONString());
				} else {
					user.setPassword(this.newPassword);
					User ref = new User();
					ref.setUsername(user.getUsername());
					if(this.userService.updateUser(ref, user)){
						out.write(JSONUtil.newSuccessJSONObject().toJSONString());
						//覆盖原session里的用户对象。因为发现直接修改取出来的user不能影响到session里的user对象
						this.getSession().put(Const.SESSION_USER, user);	
					} 
					else {
						out.write(JSONUtil.newErrMsgJSONObject("修改密码失败").toJSONString());
					}
				}
			} else {
				out.write(JSONUtil.newErrMsgJSONObject("没有提交密码参数").toJSONString());
			}
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * 用来检测当前是否处于登录状态
	 * @throws IOException 
	 */
	public void isLogin() throws IOException{
		User user = (User) this.getSession().get(Const.SESSION_USER);
		PrintWriter out = this.getWriter();
		if(user == null){
			out.write(JSONUtil.newErrMsgJSONObject("未登录").toJSONString());
		} else {
			out.write(JSONUtil.newSuccessJSONObject().toJSONString());
		}
		out.flush();
		out.close();
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

//	public Device getDevice() {
//		return device;
//	}
//
//	public void setDevice(Device device) {
//		this.device = device;
//	}

	public DeviceService getDeviceService() {
		return deviceService;
	}

	public void setDeviceService(DeviceService deviceService) {
		this.deviceService = deviceService;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
}
